layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 网上建店信息gcg_wsjd管理
     */
    var GcgWsjd = {
        tableId: "gcgWsjdTable"
    };

    /**
     * 初始化表格的列
     */
    GcgWsjd.initColumn = function () {
        return [[
            {type: 'checkbox',fixed:'left'},
            {field: 'gcgWsjdId', hide: true, title: ''},
            // {align: 'center', type: "numbers", title: '序号',fixed:'left'},
            {field: 'yaoma', title: '妖码'},
            {field: 'wsjdDm', title: '门店名称'},
            {field: 'yyName', title: '营运经理'},
            {field: 'ddName', title: '督导'},
            // {field: 'dizhiShengView', sort: true, title: '地址(省)名称'},
            // {field: 'dizhiShiView', sort: true, title: '地址(市)名称'},
            // {field: 'dizhiXianquView', sort: true, title: '地址(县区)名称'},
            // {field: 'dizhiXiangxi', title: '详细地址'},
            // {field: 'pinpai', sort: true, title: '对外门店名称(品牌)'},
            // {field: 'guishu', sort: true, title: '公司归属'},
            // {field: 'mdTel', title: '门店电话'},
            // {field: 'lxrName', sort: true, title: '门店联系人'},
            // {field: 'lxrTel', sort: true, title: '门店联系人电话'},
            // {field: 'jsTel', sort: true, title: '接受门店手机号码'},
            // {field: 'pinlei', sort: true, title: '菜系品类'},
            // {field: 'mdYysj', title: '门店营业时间'},
            // {field: 'isTcw', sort: true, title: '是否有停车位,1->有,0->无'},
            // {field: 'tcwPrice', sort: true, title: '停车位价格'},
            // {field: 'renjunPrice', sort: true, title: '人均价格'},
            // {field: 'farenXingming', title: '法人'},
            // {field: 'yhkName', title: '银行卡开户人'},
            // {field: 'yhkHao', title: '银行卡卡号'},
            // {field: 'isFapiao', sort: true, title: '是否支持开发票'},
            // {field: 'isYouhui', sort: true, title: '是否申请优惠'},
            // {field: 'farenSfzPic1', sort: true, title: '法人身份证人像面'},
            // {field: 'farenSfzPic2', sort: true, title: '法人身份证国徽面'},
            // {field: 'farenSfzPic3', sort: true, title: '法人手持身份证'},
            // {field: 'dzSfzPic1', sort: true, title: '店长身份证人像面'},
            // {field: 'dzSfzPic2', sort: true, title: '店长身份证国徽面'},
            // {field: 'dzSfzPic3', sort: true, title: '店长手持身份证'},
            // {field: 'yingyeZhizhao', sort: true, title: '营业执照'},
            // {field: 'shipinXkz', sort: true, title: '食品经营许可证'},
            // {field: 'zfbShouquan', sort: true, title: '支付宝授权函'},
            // {field: 'mentouPics', sort: true, title: '门头照'},
            // {field: 'neijingPics', sort: true, title: '内景照'},
            // {field: 'shStatus', sort: true, title: '审核状态,1->待审批，2->被驳回， 3->通过'},
            {field: 'mttg',align: 'center',title: '美团团购',templet: function (d) {
                    if (d.mttg === undefined || d.mttg === "" || d.mttg == 0) {
                        return "未完成";
                    }else if (d.mttg == 1){
                        return "已完成";
                    }
            }},
            {field: 'zfb',align: 'center',title: '支付宝',templet: function (d) {
                    if (d.zfb === undefined || d.zfb === "" || d.zfb == 0) {
                        return "未完成";
                    }else if (d.zfb == 1){
                        return "已完成";
                    }
                }},
            {field: 'mt',align: 'center',title: '美团外卖',templet: function (d) {
                    if (d.mt === undefined || d.mt === "" || d.mt == 0) {
                        return "未完成";
                    }else if (d.mttg == 1){
                        return "已完成";
                    }
                }},
            {field: 'elm',align: 'center',title: '饿了么',templet: function (d) {
                    if (d.elm === undefined || d.elm === "" || d.elm == 0) {
                        return "未完成";
                    }else if (d.mttg == 1){
                        return "已完成";
                    }
                }},
            {field: 'createTime', sort: true, title: '创建时间'},
            // {field: 'createUser', sort: true, title: 'create_user 创建人'},
            // {field: 'updateTime', sort: true, title: '修改时间'},
            // {field: 'updateUser', sort: true, title: 'update_user 修改人'},
            // {field: 'gcgWsjdYinhangkaId', sort: true, title: '网上建店银行卡id'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    GcgWsjd.search = function () {
        var queryData = {};
        queryData['wsjdDm'] = $('#wsjdDm').val();
        queryData['yaoma'] = $('#yaoma').val();

        table.reload(GcgWsjd.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    GcgWsjd.openAddDlg = function () {
        func.open({
            title: '添加网上建店信息',
            content: Feng.ctxPath + '/wsjd/add',
            tableId: GcgWsjd.tableId,
            height: 670,
            width:'800px',
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      GcgWsjd.openEditDlg = function (data) {
          func.open({
              title: '修改网上建店信息',
              content: Feng.ctxPath + '/wsjd/edit?gcgWsjdId=' + data.gcgWsjdId,
              tableId: GcgWsjd.tableId,
              height: 650,
              width:'800px',
          });
      };


    /**
     * 导出excel按钮
     */
    GcgWsjd.exportExcel = function () {
        var checkRows = table.checkStatus(GcgWsjd.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    GcgWsjd.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/wsjd/delete", function (data) {
                if(data==2){
                    Feng.error("已审批,不允许删除!");
                    return;
                }
                Feng.success("删除成功!");
                table.reload(GcgWsjd.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("gcgWsjdId", data.gcgWsjdId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + GcgWsjd.tableId,
        url: Feng.ctxPath + '/wsjd/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: GcgWsjd.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        GcgWsjd.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    GcgWsjd.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        GcgWsjd.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + GcgWsjd.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            GcgWsjd.openEditDlg(data);
        } else if (layEvent === 'delete') {
            GcgWsjd.onDeleteItem(data);
        }
    });
});
